<?php

class Model extends MVCBase
{
    public $tables = NULL;
    public $views = NULL;
    
    function  __construct()
    {
        parent::__construct();
    }

    protected function loadTable($tableName,$option = NULL,$forceLoadGeneral = TRUE)
    {
        $tableName = strtolower($tableName);
        $tableClass = $tableName . 'Table';
        if($forceLoadGeneral)
        {
            $path = APP_PATH . 'tables' . DS . $tableName . '.table.php';
        }
        else
        {
            global $_app_config;
            if(isset($_app_config['component_enable']) && $_app_config['component_enable'] == TRUE)
            {
                $path = APP_PATH . 'components' . DS . $_app_config['component_name'] . DS . 'tables' . DS . $tableName .'.table.php';
            }
            else
            {
                $path = APP_PATH . 'tables' . DS . $tableName . '.table.php';
            }
        }
        if(!$this->loadClass($tableClass,$path,FALSE))
        {
            exit("Table $tableName does not exist!");
        }
        if(is_null($option))
        {
            $tableObj = new $tableClass;
        }
        else
        {
            $rc = new ReflectionClass($tableClass);
            $tableObj = $rc->newInstance($option);
        }               
        $this->register('tables',$tableObj,$tableName);
    }

    protected function loadView($viewName,$forceLoadGeneral = FALSE)
    {
        $viewName = strtolower($viewName);
        if($forceLoadGeneral)
        {
            $path = APP_PATH . 'views' . DS . $viewName . '.view.php';
        }
        else
        {
            global $_app_config;
            if (isset($_app_config['component_enable']) && $_app_config['component_enable'] == TRUE)
            {
                $path = APP_PATH . 'components' . DS . $_app_config['component_name'] . DS . 'views' . DS . $viewName . '.view.php';
            }
            else
            {
                $path = APP_PATH . 'views' . DS . $viewName . '.view.php';
            }
        }
        if(file_exists($path))
        {
            require $path;
            $viewClass = $viewName . 'View';
            if(class_exists($viewClass))
            {
                $obj = new $viewClass;
            }
        }
        if(!isset($obj))
            $obj = new View($viewName);
        $this->register('views',$obj, $viewName);
        return $obj;
    }
}

?>
